﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class WebForm2 : System.Web.UI.Page
    {

        
        private void loaddata() {

            if (Request.QueryString["id"] == null)
            {
                var db = new DataContextDataContext().Orders.OrderByDescending(od => od.datecreation).ToList();
                if (db.Count > 0)
                {
                    lsvOrders.DataSource = db;
                    lsvOrders.DataBind();
                }
            }
            else
            {

                List<Order> list = new DataContextDataContext().Orders.Where(od => od.accountId == Convert.ToInt32(Request["ID"])).OrderByDescending(od => od.datecreation).ToList();
                if (list.Count > 0)
                {
                    lsvOrders.DataSource = list;
                    lsvOrders.DataBind();
                    Session.Remove("user");
                    Session["user"] = Request.QueryString["id"];
                }

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Orders";
            if (!Page.IsPostBack)
            {
                loaddata();  
            }
        }

        protected void lsvOrders_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
        {
            
        }
        
        protected void lsvOrders_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
           
            DataContextDataContext dc = new DataContextDataContext();
            int orderid = Convert.ToInt32((lsvOrders.Items[e.ItemIndex].FindControl("hdfOrderID") as HiddenField).Value);
            

            try
            {
                Order ord = dc.Orders.Single(or => or.id == orderid);
                ord.status = (lsvOrders.Items[e.ItemIndex].FindControl("chkStatus") as CheckBox).Checked;
                dc.SubmitChanges();
                
                // if checkbox status is true decrease quantity product
                if (ord.status == true)
                {
                    //get all productid in orderdetail = orderid
                    var productid = dc.OrderDetails.Where(od => od.orderId == orderid).ToList();
                    foreach (var item in productid)
                    {
                        //update quantity of a product
                        Product pr = dc.Products.Single(p => p.id == item.productsId);
                        pr.quantity = pr.quantity - item.quantity;
                        dc.SubmitChanges();
                    }
                }
                lblmessage.InnerText = "Update success";
            }
            catch
            {
                lblmessage.InnerText = "update failed";
            }
        }

        protected void lsvOrders_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
         
        }

        protected void lsvOrders_SelectedIndexChanged(object sender, EventArgs e)
        {
            int userid = 0;
            userid = Convert.ToInt32((lsvOrders.Items[lsvOrders.SelectedIndex].FindControl("hdfOrderID") as HiddenField).Value);
            Response.Redirect("Orderdetail.aspx?id=" + userid);
        }

        protected void lsvOrders_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            DataPager1.SetPageProperties(e.StartRowIndex, 10, false);
            loaddata();
        }

      

    }
}